extern crate webhook;
extern crate hyper;
extern crate rustc_serialize;


use std::io::Write;
use std::net::Ipv4Addr;
use std::net::SocketAddrV4;

use hyper::Server;
use hyper::server::Request;
use hyper::server::Response;
use hyper::net::Fresh;

use rustc_serialize::json;

use webhook::hook;
use webhook::hook::bitbucket;
use webhook::hook::github;
use webhook::hook::gitosc;


fn main() {
    bitbucket::hello();
    github::hello();
    gitosc::hello();

    // test only
    hook::exec_command("ls");

    let raw_json = r#"{"password":"123456","push_data":{"ref":"reference","repository":{"url":"http://git.oschina.net","ddd":12}}}"#;
    let result = gitosc::Hook::new(raw_json);
    match result {
        Ok(ref x) => {
         println!("{:?}", *x);
         println!("{}", json::encode(x).unwrap());
        }
        Err(e) => println!("{:?}", e),
    }

    // http server test
    Server::http(hello).listen(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 3000)).unwrap();
}

fn hello(_: Request, res: Response<Fresh>) {
    let mut res = res.start().unwrap();
    res.write_all(b"Hyper: Hello World!").unwrap();
    res.end().unwrap();
}
